﻿using Heima.OA.BLL;
using Heima.OA.Common;
using Heima.OA.DALFactory;
using Heima.OA.IBLL;
using Heima.OA.IDAL;
using Heima.OA.Model;
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace SyncService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
            Service1Timer.Interval = 1000 * 20;
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }

        private void Service1Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Sync();
        }

        public void Sync()
        {
            #region test
            //Log4netWriter log = new Log4netWriter();
            //log.WriterInfoLog("info info info");
            //ILog logWriter = log4net.LogManager.GetLogger("syncLogger");
            //logWriter.Info("qqqqqq"); 
            #endregion
            try
            {
                DeclareTrasferCheck();
                
            }
            catch(Exception ex)
            {
                Log4netWriter log = new Log4netWriter();
                log.WriterInfoLog(ex.Message);
            }
            finally
            {

            }


        }
        public IDbSession DbSession
        {
            get
            {
                return DbSessionFactory.GetCurrentDbSession();
            }
        }
        #region 申报转核查one
        public void DeclareTrasferCheck()
        {
            IEnumerable<ZS_DeclareInfo> declareList = DbSession.ZS_DeclareInfoDal.GetEntities(d => d.IsTransferCheck == (int)AllEnum.IsTransferCheckEnum.确认申报转核查).Take(10).AsEnumerable();
            foreach (ZS_DeclareInfo declareModel in declareList)
            {
                //转移家庭与当事人 如果已经转移就不再转
                ZS_FamilyInfo familyInfo = DbSession.ZS_FamilyInfoDal.GetModel(declareModel.FamilyId ?? 0);
                if (familyInfo != null)
                {
                    var checkFamilyInfoModel = DbSession.ZS_CheckFamilyInfoDal.GetEntities(u => u.DelFlag == 0 && u.FamilyCode.Contains(familyInfo.FamilyCode)
                    && u.FamilyAddress.Contains(familyInfo.FamilyAddress));
                    if (checkFamilyInfoModel == null)
                    {
                        //转移申报家庭到核查家庭表
                        ZS_CheckFamilyInfo checkFamilyInfo = TransferFamily(declareModel);
                        //转移申报当事人到核查当事人表
                        TransferMember(declareModel, checkFamilyInfo);
                        //核查家庭表添加户主与配偶
                        SetCheckFamily(checkFamilyInfo);
                    }
                }
                //转移楼栋与房号 如果已经转移就不再转移
                var buildingInfo = DbSession.ZS_BuildingInfoDal.GetModel(declareModel.BuildingId ?? 0);
                if (buildingInfo != null)
                {
                    var checkBuildingInfoModel = DbSession.ZS_CheckBuildingInfoDal.GetEntities(u => u.DelFlag == 0 && u.BuildingName.Contains(buildingInfo.BuildingName) &&
                u.BuildingCode.Contains(buildingInfo.BuildingCode)).FirstOrDefault();
                    if (checkBuildingInfoModel == null)
                    {
                        //转移申报楼栋到核查楼栋表
                        ZS_CheckBuildingInfo checkBuildingInfo = TransferBuilding(declareModel);
                        //转移申报房屋到核查房屋
                        TransferHouse(declareModel, checkBuildingInfo);
                    }
                }

                declareModel.IsTransferCheck = (int)AllEnum.IsTransferCheckEnum.已转核查;
                DbSession.ZS_DeclareInfoDal.Update(declareModel);
                DbSession.SaveChanges();
            }
        }
        public ZS_CheckFamilyInfo TransferFamily(ZS_DeclareInfo declareInfo)
        {
            var familyInfo = DbSession.ZS_FamilyInfoDal.GetEntities(u => u.Id == declareInfo.FamilyId).FirstOrDefault();
            if (familyInfo != null)
            {
                ZS_CheckFamilyInfo checkFamilyInfo = new ZS_CheckFamilyInfo();
                checkFamilyInfo.DeclareFamilyId = familyInfo.Id;
                checkFamilyInfo.FamilyCode = familyInfo.FamilyCode;
                checkFamilyInfo.SpouseMemberId = 0;
                checkFamilyInfo.SpouseMemberName = string.Empty;
                checkFamilyInfo.HuBie = familyInfo.HuBie;
                checkFamilyInfo.PowerMemberName = string.Empty;
                checkFamilyInfo.PowerMemberId = 0;
                checkFamilyInfo.FamilyAddress = familyInfo.FamilyAddress;
                checkFamilyInfo.RegisterNumber = familyInfo.RegisterNumber;
                checkFamilyInfo.DelFlag = 0;
                checkFamilyInfo.CreateTime = DateTime.Now;
                DbSession.ZS_CheckFamilyInfoDal.Add(checkFamilyInfo);
                DbSession.SaveChanges();
                return checkFamilyInfo;
            }
            else
            {
                return new ZS_CheckFamilyInfo();
            }
        }
        public bool TransferMember(ZS_DeclareInfo declareInfo, ZS_CheckFamilyInfo checkFamilyInfo)
        {
            var memberList = DbSession.ZS_MemberDal.GetEntities(u => u.DelFlag == 0 && u.FamilyId == declareInfo.FamilyId).ToList();
            foreach (var item in memberList)
            {
                ZS_CheckMember checkMember = new ZS_CheckMember();
                checkMember.DeclareMemberId = item.Id;
                checkMember.CheckFamilyId = checkFamilyInfo.Id;
                checkMember.MemberName = item.MemberName;
                checkMember.WithPowerRelation = item.WithPowerRelation;
                checkMember.Sex = item.Sex;
                checkMember.MaritalStatus = item.MaritalStatus;
                checkMember.Birthday = item.Birthday;
                checkMember.IDNumber = item.IDNumber;
                checkMember.IdentityType = item.IdentityType;
                checkMember.RegisterInfo = item.RegisterInfo;
                checkMember.Mobile = item.Mobile;
                checkMember.MoveIntoTime = item.MoveIntoTime;
                checkMember.DelFlag = 0;
                checkMember.CreateTime = DateTime.Now;
                DbSession.ZS_CheckMemberDal.Add(checkMember);
            }
            return DbSession.SaveChanges() > 0;
        }
        public bool SetCheckFamily(ZS_CheckFamilyInfo checkFamilyInfo)
        {
            var checkPowerMember = DbSession.ZS_CheckMemberDal.GetEntities(u => u.DelFlag == 0 && u.CheckFamilyId == checkFamilyInfo.Id && u.WithPowerRelation.Contains("户主")).FirstOrDefault();
            if (checkPowerMember != null)
            {
                checkFamilyInfo.PowerMemberId = checkPowerMember.Id;
                checkFamilyInfo.PowerMemberName = checkPowerMember.MemberName;
            }
            var checkSpouseMember = DbSession.ZS_CheckMemberDal.GetEntities(u => u.DelFlag == 0 && u.CheckFamilyId == checkFamilyInfo.Id && u.WithPowerRelation.Contains("配偶")).FirstOrDefault();
            if (checkSpouseMember != null)
            {
                checkFamilyInfo.SpouseMemberId = checkSpouseMember.Id;
                checkFamilyInfo.SpouseMemberName = checkSpouseMember.MemberName;
            }
            DbSession.ZS_CheckFamilyInfoDal.Update(checkFamilyInfo);
            return DbSession.SaveChanges() > 0;
        }
        public ZS_CheckBuildingInfo TransferBuilding(ZS_DeclareInfo declareInfo)
        {
            var buildingInfo = DbSession.ZS_BuildingInfoDal.GetModel(declareInfo.BuildingId ?? 0);
            if (buildingInfo != null)
            {
                ZS_CheckBuildingInfo checkBuildingInfo = new ZS_CheckBuildingInfo();
                checkBuildingInfo.DeclareBuildingId = buildingInfo.Id;
                checkBuildingInfo.BuildingCode = buildingInfo.BuildingCode;
                checkBuildingInfo.BuildingName = buildingInfo.BuildingName;
                checkBuildingInfo.Location = buildingInfo.Location;
                checkBuildingInfo.Floors = buildingInfo.Floors;
                checkBuildingInfo.BuildingStructure = buildingInfo.BuildingStructure;
                checkBuildingInfo.HouseUse = buildingInfo.HouseUse;
                checkBuildingInfo.ActualityUse = buildingInfo.ActualityUse;
                checkBuildingInfo.StructureArea = buildingInfo.StructureArea;
                checkBuildingInfo.MappingArea = buildingInfo.MappingArea;
                checkBuildingInfo.CompriseArea = buildingInfo.CompriseArea;
                checkBuildingInfo.JiDiArea = buildingInfo.JiDiArea;
                checkBuildingInfo.PropertyRight = buildingInfo.PropertyRight;
                checkBuildingInfo.BuiltTime = buildingInfo.BuiltTime;
                checkBuildingInfo.IsObtainNoncommodityRightCertificate = buildingInfo.IsObtainNoncommodityRightCertificate;
                checkBuildingInfo.ObtainedCertificateName = buildingInfo.ObtainedCertificateName;
                checkBuildingInfo.CertificateCode = buildingInfo.CertificateCode;
                checkBuildingInfo.CetificateArea = buildingInfo.CetificateArea;
                checkBuildingInfo.NoObtainCertificateBuiltArea = buildingInfo.NoObtainCertificateBuiltArea;
                checkBuildingInfo.Remark = buildingInfo.Remark;
                checkBuildingInfo.DelFlag = buildingInfo.DelFlag;
                checkBuildingInfo.CreateTime = DateTime.Now;
                DbSession.ZS_CheckBuildingInfoDal.Add(checkBuildingInfo);
                DbSession.SaveChanges();
                return checkBuildingInfo;
            }
            else
            {
                return new ZS_CheckBuildingInfo();
            }
        }
        public bool TransferHouse(ZS_DeclareInfo declareInfo, ZS_CheckBuildingInfo checkBuildingInfo)
        {
            var houseList = DbSession.ZS_HouseInfoDal.GetEntities(u => u.DelFlag == 0 && u.BuildingId == declareInfo.BuildingId);
            foreach (var item in houseList)
            {
                ZS_CheckHouseInfo checkHouseInfo = new ZS_CheckHouseInfo();
                checkHouseInfo.DeclareHouseId = item.Id;
                checkHouseInfo.CheckBuildingId = checkBuildingInfo.Id;
                checkHouseInfo.HouseName = item.HouseName;
                checkHouseInfo.HouseArea = item.HouseArea;
                checkHouseInfo.DecorationSituation = item.DecorationSituation;
                checkHouseInfo.ActualityUse = item.ActualityUse;
                checkHouseInfo.Remark = item.Remark;
                checkHouseInfo.DelFlag = item.DelFlag;
                checkHouseInfo.CreateTime = DateTime.Now;
                checkHouseInfo.CheckId = 0;
                checkHouseInfo.CerName = string.Empty;
                checkHouseInfo.CerCode = string.Empty;
                checkHouseInfo.RegisterRight = string.Empty;
                checkHouseInfo.RegisterBuildingArea = 0;
                checkHouseInfo.ContractSigner = string.Empty;
                checkHouseInfo.ContractRegisterBuildingArea = 0;
                checkHouseInfo.AttachmentId = 0;
                checkHouseInfo.BuildingCode = string.Empty;
                checkHouseInfo.ViolationOriginCode = string.Empty;
                checkHouseInfo.IsBuiltBeforeDate = 0;
                checkHouseInfo.BuiltAreaBeforeDate = 0;
                checkHouseInfo.BuiltAreaAfterDate = 0;
                checkHouseInfo.ViolationBuiltArea = 0;
                DbSession.ZS_CheckHouseInfoDal.Add(checkHouseInfo);
            }
            return DbSession.SaveChanges() > 0;
        }
        #endregion

        #region 申报转核查2
        public void DeclareTrasferCheckTwo()
        {
            List<ZS_FamilyInfo> familyList = DbSession.ZS_FamilyInfoDal.GetEntities(u => u.DelFlag == 0).ToList();
            foreach(var item in familyList)
            {

            }

        }
        #endregion

    }
}
